package com.liuqianfei.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liuqianfei
 */
public class StringUtil
{
    private static String EMPTY = "";

    public static boolean isEmpty(final CharSequence cs)
    {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(final CharSequence cs)
    {
        return !isEmpty(cs);
    }

    public static boolean isBlank(final CharSequence cs)
    {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0)
            return true;
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i)))
                return false;
        }
        return true;
    }

    public static boolean isNotBlank(final CharSequence cs)
    {
        return !isBlank(cs);
    }

    public static String substr(String str, int start)
    {
        if (null == str)
            return null;

        int length = str.length();

        // convert to positive
        if (start < 0)
            start += length;

        // check array bound
        if (start < 0)
            start = 0;

        if (start >= length)
            start = length;

        return str.substring(start);
    }

    public static String substr(String str, int start, int len)
    {
        if (null == str)
            return null;

        if (len < 0)
            return EMPTY;

        int length = str.length();

        // convert to positive
        if (start < 0)
            start += length;

        // check array bound
        if (start < 0)
            start = 0;

        if (start >= length)
            start = length;

        int end = start + len;
        if (end > length)
            end = length;

        return str.substring(start, end);
    }

    public static String slice(String str, int start)
    {
        return substr(str, start);
    }

    public static String slice(String str, int start, int end)
    {
        if (null == str)
            return null;

        int length = str.length();

        // convert to positive
        if (start < 0)
            start += length;

        if (end < 0)
            end += length;

        // assert
        if (start >= end)
            return EMPTY;

        // check array bound
        if (start < 0)
            start = 0;
        if (end < 0)
            start = 0;

        if (start >= length)
            start = length;
        if (end >= length)
            end = length;

        return str.substring(start, end);
    }

    public static String[] match(String regexp, String src, boolean caseInsensitive)
    {
        List<String> results = new ArrayList<>();

        Pattern pattern;
        if (caseInsensitive)
            pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
        else
            pattern = Pattern.compile(regexp);

        Matcher matcher = pattern.matcher(src);

        while (matcher.find())
        {
            results.add(matcher.group());
        }

        return results.toArray(new String[results.size()]);
    }

    public static boolean startsWithIgnoreCase(String string, String prefix)
    {
        if (null == string)
            return false;

        if (null == prefix)
            return false;

        string = string.toLowerCase();
        prefix = prefix.toLowerCase();

        return string.startsWith(prefix);
    }

    public static String toUnicode(String string)
    {
        StringBuilder unicode = new StringBuilder();

        for (int i = 0; i < string.length(); i++)
        {
            char c = string.charAt(i);
            unicode.append("\\u").append(Integer.toHexString(c));
        }

        return unicode.toString();
    }

    public static String fromUnicode(String unicode)
    {
        StringBuilder string = new StringBuilder();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++)
        {
            int data = Integer.parseInt(hex[i], 16);
            string.append((char) data);
        }

        return string.toString();
    }

    public static String toAscii(String string)
    {
        StringBuilder sb = new StringBuilder();
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++)
        {
            if(i != chars.length - 1)
                sb.append((int)chars[i]).append(",");
            else
                sb.append((int)chars[i]);
        }
        return sb.toString();
    }

    public static String fromAscii(String ascii)
    {
        StringBuilder sb = new StringBuilder();
        String[] chars = ascii.split(",");
        for (String aChar : chars)
            sb.append((char) Integer.parseInt(aChar));
        return sb.toString();
    }
}
